﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.IO;

namespace Home.Haushalt.Lib
{
    /// <summary>
    /// 
    /// </summary>
    public class SettingsCollection
    {
        /// <summary></summary>
        private const string SETTINGS_FILE_NAME = "settings.xml";

        /// <summary></summary>
        private DataSet _ds = new DataSet();

        /// <summary></summary>
        public List<CSettings> Items;

        /// <summary></summary>
        public CSettings Selected{get;private set;}

        /// <summary>
        /// 
        /// </summary>
        public SettingsCollection()
        {
            LoadData();

        }

        /// <summary>
        /// 
        /// </summary>
        private void LoadData()
        {
            if (File.Exists(SETTINGS_FILE_NAME))
            {
                _ds.ReadXml(SETTINGS_FILE_NAME);

                Items = _ds.Tables.Cast<DataTable>().Select(item => new CSettings(item)).ToList();

                Selected = Items[0];
            }
            else
            {
                Journal.Add(this, ActionResult.Nok, "Settigns-Datei [" + SETTINGS_FILE_NAME + "] ist nicht gefunden");
            }

        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="userName"></param>
        public void SetUser(string userName)
        {
            Selected = Items.Where(item => item.UserName.Equals(userName)).FirstOrDefault();
        }


        /// <summary>
        /// 
        /// </summary>
        public void SaveSettings()
        {
            _ds.WriteXml(SETTINGS_FILE_NAME);
        }
    }
}
